import request from '@/utils/request';

// 定义系统通知相关接口的类型
export interface SystemNotification {
  notification_id: number;
  title: string;
  content: string;
  type: 'info' | 'success' | 'warning' | 'error';
  sender: {
    user_id: number;
    name: string;
    avatar?: string;
  };
  sender_id?: number;
  is_sent: boolean;
  sent_at?: string;
  receive_users?: Record<string, { read: boolean }> | null;
  createdAt: string;
  updatedAt: string;
}

// 获取系统通知列表
export const getNotificationList = (data?: { page?: number; limit?: number; keyword?: string; is_sent?: number | boolean; sort_field?: string; sort_order?: string }) => {
  return request.post<{ 
    list: SystemNotification[]; 
    pagination: any 
  }>('/notification/list', data);
};

// 获取系统通知列表（无分页）
export const getNotificationListAll = (data?: { keyword?: string; is_sent?: number | boolean; sort_field?: string; sort_order?: string }) => {
  return request.post<SystemNotification[]>('/notification/listAll', data);
};

// 获取最新系统通知
export const getLatestNotifications = (data?: { limit?: number }) => {
  return request.post<SystemNotification[]>('/notification/latest', data);
};

// 创建系统通知
export const createNotification = (data: { title: string; content: string; type?: string; receive_user_ids?: number[] | null }) => {
  return request.post<SystemNotification>('/notification/create', data);
};

// 编辑系统通知
export const editNotification = (data: { notification_id: number; title: string; content: string; type: string; receive_user_ids?: number[] | null }) => {
  return request.post<SystemNotification>('/notification/edit', data);
};

// 发送系统通知
export const sendNotification = (notification_id: number) => {
  return request.post<SystemNotification>('/notification/send', { notification_id });
};

// 批量发送系统通知
export const batchSendNotifications = (notification_ids: number[]) => {
  return request.post('/notification/batch-send', { notification_ids });
};

// 批量设置系统通知为未发送状态
export const batchSetUnsent = (notification_ids: number[]) => {
  return request.post('/notification/batch-unsent', { notification_ids });
};

// 删除系统通知
export const deleteNotification = (notification_id: number) => {
  return request.post('/notification/delete', { notification_id });
};

// 批量删除系统通知
export const batchDeleteNotifications = (notification_ids: number[]) => {
  return request.post('/notification/batch-delete', { notification_ids });
};

// 获取用户未读系统通知数量
export const getUnreadNotificationCount = () => {
  return request.post<{ unread_count: number }>('/notification/unread-count');
};

// 标记系统通知为已读
export const markNotificationAsRead = (notification_id: number) => {
  return request.post('/notification/mark-read', { notification_id });
};

// 重置系统通知所有用户的已读状态
export const resetNotificationReadStatus = (notification_id: number) => {
  return request.post('/notification/reset-read-status', { notification_id });
};

// 批量重置系统通知所有用户的已读状态
export const batchResetNotificationReadStatus = (notification_ids: number[]) => {
  return request.post('/notification/batch-reset-read-status', { notification_ids });
};